package problem560;

import java.util.HashMap;

//560.和为k的子数组
//https://leetcode.cn/problems/subarray-sum-equals-k/description/?envType=study-plan-v2&envId=top-100-liked

class Solution {
    public int subarraySum(int[] nums, int k) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int sum = 0, ret = 0;
        map.put(0,1);
        for(int i = 0; i<nums.length; i++) {
            sum += nums[i];
            ret += map.getOrDefault(sum-k, 0);
            map.put(sum, map.getOrDefault(sum, 0)+1);
        }
        return ret;
    }
}